package javacommon.web.tag;

import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 分页标签处理类
 * ajax分页依赖public.js
 */
public class Pager extends SimpleTagSupport {
	private String url;//超链接目标URL
	private int pageLimit = 10;//最多显示页码数
	private String target;//用于ajax分页的目标显示区域div的id
	private String flag = "?";

	@Override
	public void doTag() throws JspException, IOException {
		PageContext context = (PageContext) getJspContext();
		ServletRequest request = context.getRequest();
		JspWriter out = context.getOut();
		// 1获得request中的页码及总页数
		int pageNo = (Integer) request.getAttribute("pageNo");
		int pageCount = (Integer) request.getAttribute("pageCount");
		flag = url.indexOf("?") > -1 ? "&" : "?";
		// 输出左边按钮
		printLeftButton(pageNo, pageCount, out);
		// 输出页码
		printPage(pageNo, pageCount, out);
		// 输出右边按钮
		printRightButton(pageNo, pageCount, out);

	}

	private void printRightButton(int pageNo, int pageCount, JspWriter out)
			throws IOException {
		if (pageNo != pageCount) {
			if (null != target) {
				out.println("<a title='尾页' href = \"" + getAjaxHref(pageCount) + "\">..."+ pageCount + "</a>");
				out.println("<a title='下一页' href = \"" + getAjaxHref(pageNo+1) + "\"> > </a>");
			} else
				out.println("<a title='尾页' href='" + getNormalHref(pageCount) + "'> ..." + pageCount + " </a>"
						+ "<a title='下一页' href='" + getNormalHref(pageNo+1) + "'> > </a>");

		}
	}

	private String getAjaxHref(int page) {
		String u = "javascript:to_load('" + target + "','"+url+flag+"pageNo="+page+"')";
		return u;
	}
	private String getNormalHref(int page){
		return url + flag + "pageNo="	+ page;
	}
	/**
	 * 
	 * @param pageNo
	 *            当前页码
	 * @param pageCount
	 *            总页数
	 * @param out
	 * @throws IOException
	 */
	private void printPage(int pageNo, int pageCount, JspWriter out)
			throws IOException {

		int start = 0;
		int end = 0;
		// 计算起始页码和终止页码
		if (pageCount / pageLimit == 0) {//
			start = 1;
			end = pageCount;
		} else {
			if (pageNo % pageLimit == 0) {
				start = (pageNo / pageLimit - 1) * pageLimit + 1;
				end = pageNo;
			} else {
				start = (pageNo / pageLimit) * pageLimit + 1;
				end = (pageNo / pageLimit + 1) * pageLimit;
			}
		}
		if (end > pageCount)
			end = pageCount;
		for (int i = start; i <= end; i++) {
			if (i == pageNo) {
				out.println("<span class = 'cur_page' >" + i + "</span>");
			} else {
				if (null!=target) {
					out.println("<a href = \"" + getAjaxHref(i) + "\">" + i + "</a>");
				} else
					out.println("<a href = '" + getNormalHref(i)+ "'>" + i + "</a>");
			}
		}
	}

	private void printLeftButton(int pageNo, int pageCount, JspWriter out)
			throws IOException {
		if (pageNo != 1) {
			if (null!=target) {
				out.println("<a title='第一页' href=\"" + getAjaxHref(1)+"\"> 1... </a>" 
						+ "<a title='上一页' href=\"" + getAjaxHref(pageNo-1) + "\"> < </a>");
			} else
				out.println("<a title='第一页' href='" + getNormalHref(1)+"'> 1... </a>" 
						+ "<a title='上一页' href='" + getNormalHref(pageNo-1) + "'> < </a>");
		}
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getPageLimit() {
		return pageLimit;
	}

	public void setPageLimit(int pageLimit) {
		this.pageLimit = pageLimit;
		if (pageLimit <= 0)
			this.pageLimit = 10;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}
}
